﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InfoLeech
{
    public partial class ctlUserTools : UserControl
    {
        private string connectionStringUser = frmMain.connectionStringUser;
        private BindingSource bindingSourceLogonHistory = new BindingSource();
        private SqlDataAdapter dataAdapterLogonHistory = new SqlDataAdapter();
        private DataTable tableLogonHistory = new DataTable();

        public ctlUserTools()
        {
            InitializeComponent();
            // Bind the DataGridView to the BindingSource
            dataGridViewLogonHistory.DataSource = bindingSourceLogonHistory;
        }

        private void GetLogonHistoryData()
        {
            if (tbUserHistory.Text.Length < 3)
            {
                MessageBox.Show("Zoekstring is te kort. Deze moet minimaal 3 karakters lang zijn.");
            }
            else
            {
                //Clear table
                try
                {
                    tableLogonHistory.Clear();
                }
                catch (DataException e)
                {
                    MessageBox.Show(e.Message);
                }

                try
                {
                    string qryLogonHistory =
                        "select RTRIM(computername) Computername, RTRIM(username) Account, RTRIM(userdescription) UserName, "
                        + "RTRIM(datum) Date, RTRIM(timebegin)StartTime from userlogging " +
                        "where username = '" + tbUserHistory.Text + "' order by Datum desc";

                    // Create a new data adapter based on the specified query.
                    dataAdapterLogonHistory = new SqlDataAdapter(qryLogonHistory, connectionStringUser);
                    tableLogonHistory.Locale = System.Globalization.CultureInfo.InvariantCulture;

                    //Fill datatable with query results.
                    dataAdapterLogonHistory.Fill(tableLogonHistory);
                    bindingSourceLogonHistory.DataSource = tableLogonHistory;
                }
                catch (SqlException)
                {
                    MessageBox.Show("Error connecting to database.");
                }
            }
        }

        private void tbUserHistory_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                GetLogonHistoryData();
            }
        }

        private void btnSearchUserHistory_Click(object sender, EventArgs e)
        {
            GetLogonHistoryData();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // The context menu strip called this method, so we need to determine which DGV it was clicked on    
            ToolStripDropDownItem item = sender as ToolStripDropDownItem;
            if (item == null) // Error            
                return;
            ContextMenuStrip strip = item.Owner as ContextMenuStrip;
            DataGridView grid = strip.SourceControl as DataGridView;
            if (grid == null) // Control wasn't a DGV
                return;
            DataObject data = grid.GetClipboardContent();
            if (data != null)
                Clipboard.SetDataObject(data);
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // The context menu strip called this method, so we need to determine which DGV it was clicked on    
            ToolStripDropDownItem item = sender as ToolStripDropDownItem;
            if (item == null) // Error            
                return;
            ContextMenuStrip strip = item.Owner as ContextMenuStrip;
            DataGridView grid = strip.SourceControl as DataGridView;
            if (grid == null) // Control wasn't a DGV
                return;
            grid.SelectAll();
        }
    }
}
